/*
 * Funambol is a mobile platform developed by Funambol, Inc.
 * Copyright (C) 2008 Funambol, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 *
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */


#import <UIKit/UIKit.h>


#import "common.h"
#import "SyncMethodsView.h"
#import "base/Log.h"
#import "UIController.h"
#import "FunambolAPI.h"

@implementation SyncMethodsView

- (id)initWithModes:(NSArray*)syncModes {
	self = [super init];
	if (self)
	{
		// this title will appear in the navigation bar
		self.title = NSLocalizedString(@"Sync Direction",@"Sync Direction");
        
        modes = syncModes;
        currentSelection = [modes objectAtIndex:0];
    }
	return self;
}

- (void) loadView{
    UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
	self.view = contentView;
    
    twoWayCell             =   [[UITableViewCell alloc]init];
    oneWayFromServerCell   =   [[UITableViewCell alloc]init];
    oneWayFromClientCell   =   [[UITableViewCell alloc]init];
    recoverFromServerCell  =   [[UITableViewCell alloc]init];
    recoverFromClientCell  =   [[UITableViewCell alloc]init];
    
    [[twoWayCell textLabel] setText:NSLocalizedString(@"Two-Way",@"Two-Way")];
    [[oneWayFromServerCell textLabel] setText:NSLocalizedString(@"One Way From Server",@"One Way From Server")];
    [[oneWayFromClientCell textLabel] setText:NSLocalizedString(@"One Way From Client",@"One Way From Client")];
    [[recoverFromServerCell textLabel] setText:NSLocalizedString(@"Recover From Server",@"Recover From Server")];
    [[recoverFromClientCell textLabel] setText:NSLocalizedString(@"Recover From Client",@"Recover From Client")];
    
    NSMutableDictionary * cells = [[NSMutableDictionary alloc] init];
    [cells setObject:twoWayCell            forKey:@"two-way"];
    [cells setObject:oneWayFromServerCell  forKey:@"one-way-from-server"];
    [cells setObject:oneWayFromClientCell  forKey:@"one-way-from-client"];
    [cells setObject:recoverFromServerCell forKey:@"recover-from-server"];
    [cells setObject:recoverFromClientCell forKey:@"recover-from-client"];
    cellMap = [cells copy];
    [cells release];
    
    table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped]; 
	table.delegate = self;
    table.dataSource = self;
	table.rowHeight = 40;
    [table reloadData];
    
    [self.view addSubview:table];
}

- (void)viewDidAppear:(BOOL)animated{
    NSString* val = [[FunambolAPI getInstance] getSync:name];
    if([val isEqualToString:@"two-way"]){
        [twoWayCell setAccessoryType:UITableViewCellAccessoryCheckmark];
        [oneWayFromClientCell setAccessoryType:UITableViewCellAccessoryNone];
        [oneWayFromServerCell setAccessoryType:UITableViewCellAccessoryNone];
    }else if([val isEqualToString:@"one-way-from-server"]){
        [twoWayCell setAccessoryType:UITableViewCellAccessoryNone];
        [oneWayFromClientCell setAccessoryType:UITableViewCellAccessoryNone];
        [oneWayFromServerCell setAccessoryType:UITableViewCellAccessoryCheckmark];
    }else if([val isEqualToString:@"one-way-from-client"]){
        
        [twoWayCell setAccessoryType:UITableViewCellAccessoryNone];
        [oneWayFromClientCell setAccessoryType:UITableViewCellAccessoryCheckmark];
        [oneWayFromServerCell setAccessoryType:UITableViewCellAccessoryNone];
    }
}

//TABLE VIEW

- (UITableViewCellAccessoryType)tableView:(UITableView *)table accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{

    NSString * value = [modes objectAtIndex:indexPath.row];
    
    if ([value isEqual:currentSelection])
    {
        return UITableViewCellAccessoryCheckmark;
    }
    else
    {
        return UITableViewCellAccessoryNone;
    }
}

- (void)tableView:(UITableView *)settingsTable didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [[cellMap objectForKey:currentSelection] setAccessoryType:UITableViewCellAccessoryNone];
    currentSelection = [modes objectAtIndex:indexPath.row];
    [[cellMap objectForKey:currentSelection] setAccessoryType:UITableViewCellAccessoryCheckmark];
    
    if ([currentSelection isEqual:@"two-way"])
    {
        [self twoWayAction:self];
    }
    else if ([currentSelection isEqual:@"one-way-from-server"])
    {
        [self oneWayFromServerAction:self];
    }
    else if ([currentSelection isEqual:@"one-way-from-client"])
    {
        [self oneWayFromClientAction:self];
    }
    else if ([currentSelection isEqual:@"recover-from-server"])
    {
        [self recoverFromServerAction:self];
    }
    else if ([currentSelection isEqual:@"recover-from-client"])
    {
        [self recoverFromClientAction:self];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
	// this UIViewController is about to re-appear, make sure we remove the current selection in our table view
	NSIndexPath *tableSelection = [table indexPathForSelectedRow];
	[table deselectRowAtIndexPath:tableSelection animated:NO];
}

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * idx = [modes objectAtIndex:indexPath.row];
    UITableViewCell * ret = [cellMap objectForKey: idx ];
    return ret;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    return [modes count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)table {
    return 1;
}

- (void)reload {
    [self init];
}


- (void)dealloc{
    [table                 release];
    [twoWayCell            release];
    [oneWayFromServerCell  release];
    [oneWayFromClientCell  release];
    [recoverFromServerCell release];
    [recoverFromClientCell release];
    
    [cellMap               release];
    [modes                 release];
    [currentSelection      release];
    
    [super                 dealloc];
}

- (void)setSyncSourceName:(NSString*)sname{
    name = sname;
}

- (void)setSyncMode:(NSString*)mode
{
    if (currentSelection)
    {
        if (mode == currentSelection)
        {
            return;
        }
        
        [currentSelection release];
    }
    
    currentSelection = [mode copy];
}

- (NSString*)getSyncMode
{
    NSString * ret = [currentSelection copy];
    return ret;
}

- (void)twoWayAction:(id)sender
{
    //Subclassed
    //[[FunambolAPI getInstance] setSync:@"two-way"];
}

- (void)oneWayFromServerAction:(id)sender
{
    //Subclassed
    //[[FunambolAPI getInstance] setSync:@"one-way-from-server"];
}

- (void)oneWayFromClientAction:(id)sender
{
    //Subclassed
    //[[FunambolAPI getInstance] setSync:@"one-way-from-client"];
}

- (void)recoverFromServerAction:(id)sender
{
    //Subclassed
    //[[FunambolAPI getInstance] setRecoverMode:@"recover-from-server"];
}

- (void)recoverFromClientAction:(id)sender
{
    //Subclassed
    //[[FunambolAPI getInstance] setRecoverMode:@"recover-from-client"];
}
@end
